package com.lm.controller;

import com.lm.service.LmProductCashService;
import com.meirengu.common.StatusCode;
import com.meirengu.controller.BaseController;
import com.meirengu.model.Page;
import com.meirengu.model.Result;
import com.meirengu.utils.DateUtils;
import com.meirengu.utils.NumberUtil;
import com.meirengu.utils.StringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("productCast")
public class LmProductCashController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(LmProductCashController.class);

    @Autowired
    LmProductCashService lmProductCashService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Result listMultiple(@RequestParam(value = "page_num", required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "page_size", required = false, defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "sortby", required = false) String sortBy,
                               @RequestParam(value = "order", required = false) String order,
                               @RequestParam(value = "loan_quota_min", required = false) BigDecimal loanQuotaMin,
                               @RequestParam(value = "loan_quota_max", required = false) BigDecimal loanQuotaMax,
                               @RequestParam(value = "load_duration_min", required = false) Integer loadDurationMin,
                               @RequestParam(value = "load_duration_max", required = false) Integer loadDurationMax,
                               @RequestParam(value = "product_crowd", required = false) Integer productCrowd,
                               @RequestParam(value = "status", required = false, defaultValue = "1") Integer status,
                               @RequestParam(value = "optimization", required = false) Integer optimization,
                               @RequestParam(value = "synchronizedWx", required = false) String synchronizedWx) {

        try {
            // 目前用城市名称，后期改为用行政代码查询
            Map<String, Object> paramMap = new HashedMap();
            Page page = super.setPageParams(pageNum, pageSize);
            if (StringUtils.isNotBlank(sortBy)) {
                paramMap.put("sortBy", sortBy);
            }
            if (StringUtils.isNotBlank(order)) {
                paramMap.put("order", order);
            }
            if (!NumberUtil.isNullOrZero(loanQuotaMin)) {
                paramMap.put("loanQuotaMin", loanQuotaMin);
            }
            if (!NumberUtil.isNullOrZero(loanQuotaMax)) {
                paramMap.put("loanQuotaMax", loanQuotaMax);
            }
            if (!NumberUtil.isNullOrZero(loadDurationMin)) {
                paramMap.put("loadDurationMin", loadDurationMin);
            }
            if (!NumberUtil.isNullOrZero(loadDurationMax)) {
                paramMap.put("loadDurationMax", loadDurationMax);
            }
            if (!NumberUtil.isNullOrZero(productCrowd)) {
                paramMap.put("productCrowd", productCrowd);
            }
            if (!NumberUtil.isNullOrZero(optimization)) {
                paramMap.put("optimization", optimization);
            }
            if (StringUtils.isNotBlank(synchronizedWx)) {
                paramMap.put("synchronizedWx", synchronizedWx);
            }
            paramMap.put("status", status);
            page = lmProductCashService.getListByPage(page, paramMap);
            List<Map<String, Object>> productList = page.getList();
            if (productList != null && productList.size() > 0) {
                for (Map<String, Object> map : productList) {
                    Random random = new Random();
                    int baseNum = 0;
                    int increateNum = 0;
                    int applyTotal = 0;
                    if (map.containsKey("applyBaseNum")) {
                        baseNum = StringUtil.getIntValue(map.get("applyBaseNum").toString());
                    }
                    if (map.containsKey("applyDailyNum") && map.get("applyDailyNum") != null) {
                        increateNum = (int) StringUtil.getDoubleValue(map.get("applyDailyNum").toString());
                    }
                    int diff = (int) DateUtils.getDaySub(map.get("createDate").toString(), DateUtils.getNowTimestamp().toString());
                    int round = random.nextInt(1000);
                    applyTotal = baseNum + diff * increateNum + round;
                    //logger.debug("baseNum:"+baseNum+" increateNum:"+increateNum+"maxAmount:"+maxAmount);
                    map.put("applyTotal", applyTotal);
                }
            }
            return super.setResult(StatusCode.OK, page, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            String params = String.format("参数:loanQuotaMin:%s,loanQuotaMax:%s,loadDurationMin:%s,loadDurationMin:%s," +
                            "productCrowd:%s,status:%s,optimization:%s", loanQuotaMin, loanQuotaMax, loadDurationMin, loadDurationMin,
                    productCrowd, status, optimization);
            logger.error("LmProductCashController listMultiple throws Exceprion :{} happens. params:{}.", ExceptionUtils.getFullStackTrace(e), params);
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }

    }
}
